<?php
require_once   BaseUtil::getModulePath("base")  .  '/controllers/BaseController.php';
require_once   BaseUtil::getModulePath("gift")  .  '/services/GiftService.php';
require_once   BaseUtil::getModulePath("gift")  .  '/constants/GiftConstant.php';
require_once   BaseUtil::getModulePath("gift")  .  '/forms/GiftForm.php';

class Gift_GiftController extends BaseController {

	public function init(){
		parent::init();
		$this->view->script= array('lib/ui.dialog.js','gift.js');
	}
	
	public function indexAction(){
		$giftService = new GiftService();
		$gifts = $giftService->getGiftList($_POST,$this->view);
		$this->view->gifts = $gifts;
	}
	
	public function addAction(){
		$form = new GiftForm();
		$form->setAction(HttpUtil::getUrl('gift','subadd'));
		$this->view->form = $form;
	}
	
	public function subaddAction(){
	    $form = new GiftForm();
		if(!$form->isValid($_POST)){
			$form->setAction(HttpUtil::getUrl('gift','subedit'));
			$this->view->form = $form;
			return $this->render('add');
		}
		$giftType = HttpUtil::getPostString('gift_type');
		if($giftType == 1){
			$gift['product_item_id'] = HttpUtil::getPostString('product_item_id');
			$gift['title'] = HttpUtil::getPostString('product_title');
		} else if($giftType == 2){
		   $result = BaseUtil::uploadFile(Zend_Registry::get('gift_image_path'),'image');
		   if(isset($result['error'])){
		   	  $form->getElement('image')->addErrorMessage($result['error']);
		   	  $form->setAction(HttpUtil::getUrl('gift','subedit'));
			  $this->view->form = $form;
			  return $this->render('edit');
		   	  
		   } else if(isset($result['file_path'])){
		   	   $gift['image_path'] = $result['file_path'];
		   }
			$gift['title'] = HttpUtil::getPostString('title');
			//$gift['image_path'] = HttpUtil::getPostString('image_path');
			$gift['description'] = HttpUtil::getPostString('description');
		}
		$gift['exhaust_points'] = HttpUtil::getPostString('exhaust_point');
		$gift['min_ac_points'] = HttpUtil::getPostString('min_ac_point');
		$gift['start_date'] = HttpUtil::getPostString('start_date');
		$gift['end_date'] = HttpUtil::getPostString('end_date');
		$giftService = new GiftService();
		$giftId = $giftService->addGift($gift);
		$this->_redirector->gotoUrl('/gift/gift');
	}
	
	public function editAction(){
		$id = $this->_getParam('id');
		$giftService = new GiftService();
		$gift = $giftService->getGiftById($id);
		if($gift['product_item_id'] > 0){
			$giftType = 1;
		} else {
			$giftType = 2;
		}
		$form = new GiftForm();
		$form->setAction(HttpUtil::getUrl('gift','subedit'));
		$form->getElement('gift_type')->setValue($giftType);
		if($giftType == 2){
			$form->getElement('title')->setValue($gift['title']);
			$this->view->imagePath = $gift['image_path'];
			$form->getElement('description')->setValue($gift['description']);
		} else if($giftType == 1) {
			$this->view->producTitle = $gift['title'];
			$this->view->productItemId = $gift['product_item_id'];
		}
		
		$form->getElement('exhaust_point')->setValue($gift['exhaust_points']);
		$form->getElement('min_ac_point')->setValue($gift['min_ac_points']);
		$form->getElement('start_date')->setValue($gift['start_date']);
		$form->getElement('end_date')->setValue($gift['end_date']);
		$this->view->form = $form;
		$this->view->id = $id;
		$this->view->giftType = $giftType;	
	}
	
	public function subeditAction(){
		$id = HttpUtil::getPostString('gift_id');
		$form = new GiftForm();
		if(!$form->isValid($_POST)){
			$form->setAction(HttpUtil::getUrl('gift','subedit'));
			$this->view->form = $form;
			return $this->render('edit');
		}
		
		$giftType = HttpUtil::getPostString('gift_type');
		if($giftType == 1){
			$gift['product_item_id'] = HttpUtil::getPostString('product_item_id');
			$gift['title'] = HttpUtil::getPostString('product_title');
			$gift['image_path'] = '';
			$gift['description'] = '';
		} else if($giftType == 2){
			$result = BaseUtil::uploadFile(Zend_Registry::get('gift_image_path'),'image');
			$oldImage = HttpUtil::getPostString('old_image_name');
			$fullPath = str_replace('\\','/',Zend_Registry::get('gift_image_path')).'/'.$oldImage;
			if($oldImage && file_exists($fullPath)){
				unlink($fullPath);
			}
		   if(isset($result['error'])){
		   	  $form->getElement('image')->addErrorMessage($result['error']);
		   	  $form->setAction(HttpUtil::getUrl('gift','subedit'));
			  $this->view->form = $form;
			  return $this->render('edit');
		   	  
		   } else if(isset($result['file_path'])){
		   	   $gift['image_path'] = $result['file_path'];
		   }
			$gift['title'] = HttpUtil::getPostString('title');
			//$gift['image_path'] = HttpUtil::getPostString('image_path');
			$gift['description'] = HttpUtil::getPostString('description');
			$gift['product_item_id'] = '';
		}
		$gift['exhaust_points'] = HttpUtil::getPostString('exhaust_point');
		$gift['min_ac_points'] = HttpUtil::getPostString('min_ac_point');
		$gift['start_date'] = HttpUtil::getPostString('start_date');
		$gift['end_date'] = HttpUtil::getPostString('end_date');
		$giftService = new GiftService();
		$giftService->updateGift($gift,$id);
		$this->_redirector->gotoUrl('/gift/gift');
		
	}
	
	public function deleteAction(){
		$id = $this->_getParam('id');
		$giftService = new GiftService();
		$giftService->deleteGift($id);
		$this->_redirector->gotoUrl('/gift/gift');
	}
	
}
?>